﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public class ChiTietXuatHuy 
    {

        public static Entities.ChiTietXuatHuy[] Select(Entities.ChiTietXuatHuy nv)
        {
            Entities.ChiTietXuatHuy[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietPhieuXuatHuy.Select;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.MaPhieuXuatHuy, SqlDbType.VarChar).Value = nv.MaPhieuXuatHuy;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietXuatHuy chitietphieuxuathuy = new Entities.ChiTietXuatHuy();
                    chitietphieuxuathuy.ChiTietXuatHuyID = int.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.ChiTietXuatHuyID].ToString());
                    chitietphieuxuathuy.MaPhieuXuatHuy = dr[Common.Constants.ChiTietPhieuXuatHuy.MaPhieuXuatHuy].ToString();
                    chitietphieuxuathuy.MaHangHoa = dr[Common.Constants.ChiTietPhieuXuatHuy.MaHangHoa].ToString();
                    chitietphieuxuathuy.TenHangHoa = dr[Common.Constants.ChiTietPhieuXuatHuy.TenHangHoa].ToString();
                    chitietphieuxuathuy.SoLuong = int.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.SoLuong].ToString());
                    chitietphieuxuathuy.GiaNhap = dr[Common.Constants.ChiTietPhieuXuatHuy.GiaNhap].ToString();
                    chitietphieuxuathuy.HanSuDung = DateTime.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.HanSuDung].ToString());
                    chitietphieuxuathuy.GhiChu = dr[Common.Constants.ChiTietPhieuXuatHuy.GhiChu].ToString();
                    chitietphieuxuathuy.Deleted = bool.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.Deleted].ToString());
                    arr.Add(chitietphieuxuathuy);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietXuatHuy[0];
                }
                else
                {
                    list = new Entities.ChiTietXuatHuy[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietXuatHuy)arr[i];
                    }
                }
              
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietXuatHuy[0]; }
            return list;
        }

        public static Entities.ChiTietXuatHuy[] Select1(Entities.ChiTietXuatHuy nv)
        {
            Entities.ChiTietXuatHuy[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietPhieuXuatHuy.Select1;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietXuatHuy chitietphieuxuathuy = new Entities.ChiTietXuatHuy();
                    chitietphieuxuathuy.ChiTietXuatHuyID = int.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.ChiTietXuatHuyID].ToString());
                    chitietphieuxuathuy.MaPhieuXuatHuy = dr[Common.Constants.ChiTietPhieuXuatHuy.MaPhieuXuatHuy].ToString();
                    chitietphieuxuathuy.MaHangHoa = dr[Common.Constants.ChiTietPhieuXuatHuy.MaHangHoa].ToString();
                    chitietphieuxuathuy.TenHangHoa = dr[Common.Constants.ChiTietPhieuXuatHuy.TenHangHoa].ToString();
                    chitietphieuxuathuy.SoLuong = int.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.SoLuong].ToString());
                    chitietphieuxuathuy.GiaNhap = dr[Common.Constants.ChiTietPhieuXuatHuy.GiaNhap].ToString();
                    chitietphieuxuathuy.HanSuDung = DateTime.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.HanSuDung].ToString());
                    chitietphieuxuathuy.GhiChu = dr[Common.Constants.ChiTietPhieuXuatHuy.GhiChu].ToString();
                    chitietphieuxuathuy.Deleted = bool.Parse(dr[Common.Constants.ChiTietPhieuXuatHuy.Deleted].ToString());
                    arr.Add(chitietphieuxuathuy);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietXuatHuy[0];
                }
                else
                {
                    list = new Entities.ChiTietXuatHuy[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietXuatHuy)arr[i];
                    }
                }

                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietXuatHuy[0]; }
            return list;
        }
        // insert update
        public static int InsertUpdate(Entities.ChiTietXuatHuy chitietphieuxuathuy)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietPhieuXuatHuy.InsertUpdate, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.HanhDong, SqlDbType.NVarChar, 50).Value = chitietphieuxuathuy.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.ChiTietXuatHuyID, SqlDbType.Int).Value = chitietphieuxuathuy.ChiTietXuatHuyID;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.MaPhieuXuatHuy, SqlDbType.VarChar, 50).Value = chitietphieuxuathuy.MaPhieuXuatHuy;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.MaHangHoa, SqlDbType.VarChar, 50).Value = chitietphieuxuathuy.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.TenHangHoa, SqlDbType.NVarChar, 200).Value = chitietphieuxuathuy.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.SoLuong, SqlDbType.Int).Value = chitietphieuxuathuy.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.GiaNhap, SqlDbType.Float).Value = chitietphieuxuathuy.GiaNhap;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.HanSuDung, SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.GhiChu, SqlDbType.NVarChar, 200).Value = chitietphieuxuathuy.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.Deleted, SqlDbType.Bit).Value = chitietphieuxuathuy.Deleted;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }
        // insert update
        public static int InsertUpdate(Entities.ChiTietXuatHuy[] chitietphieuxuathuy)
        {
            try
            {
                foreach (Entities.ChiTietXuatHuy item in chitietphieuxuathuy)
                {
                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public static int Delete(Entities.ChiTietXuatHuy chitietphieuxuathuy)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietPhieuXuatHuy.Delete, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuXuatHuy.MaPhieuXuatHuy, SqlDbType.NVarChar, 50).Value = chitietphieuxuathuy.MaPhieuXuatHuy;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }
    }
}
